package com.itheima.homework08;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Scanner;

public class Activity {
    //创建键盘输入对象
    Scanner sc = new Scanner(System.in);
    //抢购时间
    LocalTime now = LocalTime.now();
    String beginTime1 = "14:00:00";
    String beginTime2 = "16:00:00";
    String beginTime3 = "18:00:00";
    String beginTime4 = "20:00:00";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
    LocalTime bgTime1 = LocalTime.parse(beginTime1, formatter);
    LocalTime bgTime2 = LocalTime.parse(beginTime2, formatter);
    LocalTime bgTime3 = LocalTime.parse(beginTime3, formatter);
    LocalTime bgTime4 = LocalTime.parse(beginTime4, formatter);

    public void goods(ArrayList<Goods> list) {
        System.out.println("JD秒杀....");
        while (true) {
            for (int i = 0; i < list.size(); i++) {
                System.out.println("商品序号" + (i + 1) + "：" + list.get(i));
            }
            System.out.println("选择需要秒杀的商品：");
            String name = sc.next();
            switch (name) {
                case "鸭蛋":
                    if (now.isAfter(bgTime1) && now.isBefore(bgTime2)) {
                        purchaseGoods(list, 0);
                    } else {
                        System.out.println("鸭蛋秒杀活动时间已经结束，您可以去抢购其他正在秒杀的商品~~");
                    }
                    break;
                case "摇头风扇":
                    if (now.isAfter(bgTime2) && now.isBefore(bgTime3)) {
                        purchaseGoods(list, 1);
                    } else {
                        System.out.println("摇头风扇秒杀活动时间已经结束，您可以去抢购其他正在秒杀的商品~~");
                    }
                    break;
                case "瓢虫项链":
                    if (now.isAfter(bgTime2) && now.isBefore(bgTime3)) {
                        purchaseGoods(list, 2);
                    } else {
                        System.out.println("瓢虫项链秒杀活动时间已经结束，您可以去抢购其他正在秒杀的商品~~");
                    }
                    break;
                default:
                    System.out.println("本次秒杀活动暂无此商品，您可以购买其他正在进行秒杀活动的商品~~");
            }
        }
    }

    //购买商品
    public void purchaseGoods(ArrayList<Goods> list, int i) {
        if (list.get(i).getInventoryQuantity() != 0) {
            System.out.println(list.get(i).getName() + "库存有" + list.get(i).getInventoryQuantity() + "个");
            int num = 0;
            while (true) {
                System.out.println("请输入您要抢购的数量：");
                num = sc.nextInt();
                if (num > list.get(i).getInventoryQuantity()) {
                    System.out.println("不好意思，您输入的数量超出了库存量，请重新输入库存以内的抢购数量~~");
                } else {
                    break;
                }
            }
            list.get(i).setInventoryQuantity(list.get(i).getInventoryQuantity() - num);
            System.out.println("恭喜您秒杀成功，" + list.get(i).getName() + "库存还剩" + list.get(i).getInventoryQuantity() + "个");
        } else {
            System.out.println(list.get(i).getName() + "已售罄！");
        }
            goods(list);
    }
}
